package org.groupg.practice.guiPractice.awt;

import cn.hutool.core.lang.Console;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.InvocationTargetException;

public class SimpleFrameTest {
    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        EventQueue.invokeAndWait(() -> {
            var frame = new SimpleFrame();
//            frame.add(new NotHelloWorldComponent());
        });
    }
}

class SimpleFrame extends JFrame {
    // 获取当前桌面的屏幕
    static Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    private static final int DEFAULT_WIDTH = dimension.width / 4 * 3;
    private static final int DEFAULT_HEIGHT = dimension.height / 4 * 3;

    public SimpleFrame() {
        // 设置窗口大小
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        // 设置窗口位置 （正中处理法
//        setLocation((dimension.width / 2) - (DEFAULT_WIDTH / 2), (dimension.height / 2) - (DEFAULT_HEIGHT / 2));
        setLocationByPlatform(true);

        // 设置关闭按钮点击关闭时是直接关掉进程还是如何
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置可见
        setVisible(Boolean.TRUE);
        // 设置标题
        setTitle("项目助手");

        // 获取图标图像
        Image img = new ImageIcon(this.getClass().getResource("/pics/pic1/1.png"), "项目助手").getImage();
        // 设置图标
        setIconImage(img);

        // 用户可以调整窗口大小
        setResizable(Boolean.TRUE);

        // 添加组件
        add(new NotHelloWorldComponent(200,200));
        add(new NotHelloWorldComponent(400,100));
        // 调整窗口大小，自适应组件大小
//        pack();
    }


}

/**
 * 画板
 */
class NotHelloWorldComponent extends JComponent {

    public static final int message_x = 75;
    public static final int message_y = 75;

    public int default_width = 300;
    public int default_height = 200;

    public NotHelloWorldComponent(int default_width ,int default_height){
        this.default_width = default_width;
        this.default_height = default_height;
    }

    /**
     * 当组件被重新绘制时自动运行，窗口发生变化时被调用
     * @param g the <code>Graphics</code> object to protect
     */
    @Override
    public void paintComponent(Graphics g) {
        // 在组件指定位置上绘制一个字符串
        g.drawString("李庚谱",message_x,message_y);
        var floatRect = new Rectangle2D.Float(10.0F,25.0F,22.5F,20.0F);
        var doubleRect = new Rectangle2D.Double(10.0,25.0,22.5,20.0);
    }

    /**
     *
     * @return
     */
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(default_width,default_height);
    }

    @Override
    public void repaint() {
        Console.log("repaint");
        super.repaint();
    }
}
